*This file creates Panel A of Table D.19

*Import data with local information on railways, above 40 years of edu, and income
use "$path/heterogeneity.dta", clear
destring mun39, gen(code)

rename railway railway_dummy
tempfile hetero
save `hetero'

*Use the previously created local-level industry dataset
use "$path/elinkeino_forregs2.dta", clear
joinby code using `hetero', unmatched(master)

*Define controls
global baseline2 year#c.manushare_a1940 year#c.agrishare_a1940

global enet_11  c.agrishare_a1940#year  c.manushare_a1940#year c.income_tax_payers38#year 

global baseline3 year#c.manushare_a1940 year#c.agrishare_a1940 year#high_yos year#high_income year#railway_dummy 

*Generate interactions
gen bartik_railway_post=bartik_st*railway_dummy*post
gen bartik_hi_post=bartik_st*railway_dummy*high_income*post
gen bartik_hedu_post=bartik_st*railway_dummy*high_yos*post

*Heterogeneity analysis
foreach x in manushare_a   agrishare_a  {
reghdfe `x'  bartik_post [aw=wei], a(year#laani50  $baseline3 year#town year#city code) cl(code)
estadd local enet " "
estadd local Municipalities "346" 
sum `x'
estadd scalar ymean=r(mean)
estadd local established "Yes"
estimates store h`x'1 
}

foreach x in manushare_a   agrishare_a  {
reghdfe `x'  bartik_post bartik_hi_post [aw=wei], a(year#laani50  $baseline3 year#town year#city code) cl(code)
estadd local enet " "
estadd local Municipalities "346" 
sum `x'
estadd scalar ymean=r(mean)
estadd local established "Yes"
estimates store h`x'2 
}

foreach x in manushare_a   agrishare_a  {
reghdfe `x'  bartik_post  bartik_railway_post [aw=wei], a(year#laani50  $baseline3 year#town year#city code) cl(code)
estadd local enet " "
estadd local Municipalities "346" 
sum `x'
estadd scalar ymean=r(mean)
estadd local established "Yes"
estimates store h`x'3
}

foreach x in manushare_a   agrishare_a  {
reghdfe `x'  bartik_post bartik_hedu_post [aw=wei], a(year#laani50  $baseline3 year#town year#city code) cl(code)
estadd local enet " "
estadd local Municipalities "346" 
sum `x'
estadd scalar ymean=r(mean)
estadd local established "Yes"
estimates store h`x'4
}

*Label variables
label var bartik_post "Exposure x Post"
label var bartik_hedu_post "Exposure x Post x High Education"
label var bartik_railway_post "Exposure x Post x Railway"
label var bartik_hi_post "Exposure x Post x High Income"


esttab  hmanushare_a1 hmanushare_a2 hmanushare_a3 hmanushare_a4 hagrishare_a1 hagrishare_a2 hagrishare_a3 hagrishare_a4 ///
using   "$tables/DD_estimates_municipality_hetero.tex",  ///
mgroups("Manufacturing Employment Share" "Agriculture Employment Share"   ,   pattern(1 0 0 0 1 0 0 0)  prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}) ) ///
replace  keep(bartik_post  bartik_hedu_post bartik_railway_post bartik_hi_post) nonotes ///
b(%9.3f) se(%9.3f) stats(N Municipalities , fmt(0 3) layout(@ @) ///
labels("N" "Municipalities"   ///
)) ///
label star(* .1 ** .05 *** .01) nodepvars nomtitle 

